
/**  
 * @Title: LoggerServerHandler.java
 * @Package com.uoamp.agent.logger.reciver
 * @Description: TODO(用一句话描述该文件做什么)
 * @author qinanyuan
 * @date 2017年4月5日 上午10:00:31
 * @version V1.0  
 */
    
package com.iflytek.uoamp.agent.logger.reciver;

import org.apache.log4j.Logger;

import com.iflytek.uoamp.agent.core.sender.loggersender.LoggerSendMethod;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.ReferenceCountUtil;


/**
 * @ClassName: LoggerServerHandler
 * @Description: LoggerServer处理类
 * @author qinanyuan
 * @date 2017年4月5日 上午10:00:31
 */

public class LoggerServerHandler extends SimpleChannelInboundHandler<byte[]>{

	private LoggerSendMethod loggerSend = new LoggerSendMethod();
	
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		Logger.getRootLogger().info("logger client is inactive!");
	}
	
	
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, byte[] msg) throws Exception {
		loggerSend.sendLogger(msg);
	}
	
	
	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		if (evt instanceof IdleStateEvent) {
			handleHeartbreat(ctx ,evt);
		}
	}

	/**
	 * 处理心跳包
	 *
	 * @param ctx
	 * @param packet
	 */
	private void handleHeartbreat(ChannelHandlerContext ctx, Object packet) {
		// 将心跳丢失计数器置为0
		Logger.getRootLogger().info("receive logger heartbeat");
		ReferenceCountUtil.release(packet);
	}
}
